<?php namespace Admin\Controller;
/**
 * 图片主题管理
 *
 */
class ThemeController extends CommonController{
		private $theme = null;
		public function __init(){
			parent::__init();
			$this->theme = new \Common\Model\Theme();
		}
		public function index(){
				$page = Page::row(8)->make($this->theme->count());
				$data = $this->theme->limit(Page::limit())->get();
				foreach ($data as $k=>$v){
						$data[$k]['total'] = Db::table('picture')->where(array('theme_id'=>$v['thid']))->count();
				}
				$this->assign('page',$page);
				$this->assign('total',$this->theme->count());
				$this->assign('data',$data);
				$this->display();
		}
		
		public function add(){
				if (IS_POST) {
					if (!$this->theme->create()) $this->error($this->theme->getError());
					
					if ($this->theme->add()) {
						$this->success('添加成功,正在为您跳转...',U('index'));
					}
				}
				$this->display();
		}
		
		public function edit(){
				if (IS_POST) {
						if (!$this->theme->create()) $this->error($this->theme->getError());
						$data = array(
								'theme' => Q('post.theme'),
								'tsort' => Q('post.tsort'),
								'animation'=>trim(Q('post.animation'))
								);
						$thid = Q('post.thid',0,'intval');
						$this->theme->where(array('thid'=>$thid))->save($data);
						$this->success('编辑成功,马上为您跳转...',U('index'));
						
				}
				$thid = Q('get.thid',0,'intval');
				$this->assign('oldData',$this->theme->where(array('thid'=>$thid))->find());
				$this->display();
		}
		
		public function del(){
			$thid = Q('get.thid',0,'intval');
			if ($this->theme->delete($thid)) $this->success('删除成功',U('index'));
		}
}
